package SwingView.Announcement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;

import JDBC.StudentAnnouncementActivities;
import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsAnnouncement;
import SwingModel.CmsLecture;
import SwingView.CmsContentPanelModel;
public class CmsCPStudentAnnouncement extends CmsContentPanelModel{

	private JLabel jlTitle;
	private JPanel jpContent;
	
	
	public CmsCPStudentAnnouncement(){
		
		//instantiate People title
		jlTitle = new JLabel("Announcements");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(650,40));
		jp.setOpaque(false);
		jp.setLayout( new FlowLayout(FlowLayout.CENTER));
		jp.setBorder(BorderFactory.createEmptyBorder());
		jp.add(jlTitle);
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);
		
		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);
		
		//set up student Announcement panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		add(scroll,BorderLayout.WEST);//Add People List
	}
	
	public void displayListOfAnnouncements(List<CmsAnnouncement> listOfAnnouncements){
		jpContent.removeAll();
		int i = 0;
		for(CmsAnnouncement announcement : listOfAnnouncements){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0, 0, 8, 0);
			
			jpContent.add(new AnnouncementEntryRow(announcement),c);
			
			i++;
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = i;
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
	}
	
	private class AnnouncementEntryRow extends JPanel{
		final static private int ROW_WIDTH = 550;
		final static private int ROW_HEIGHT = 50;
		
		final static private int DATE_NAME_COLUMN_SIZE = 100;
		private JButton viewButton;
		private JPanel jpAnmContent;
		
		private CmsAnnouncement anm;
		
		public AnnouncementEntryRow(CmsAnnouncement anm){

			JLabel jlTitle;
			JLabel postedDate; //avai date
			JLabel pdTitle;
			JLabel expDate; //exp date
			JLabel edTitle;
			
			SimpleDateFormat sdf;
			
			this.anm = anm;
			
			//Set up file name label
			String anmTitle = anm.getAnnouncementTitle();
			jlTitle = new JLabel(anmTitle);
			jlTitle.setFont(jlTitle.getFont().deriveFont(15.0f).deriveFont(Font.BOLD));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 5));
			
			//Set up the "Available date" title label
			pdTitle = new JLabel("Posted Date: ");
			pdTitle.setFont(pdTitle.getFont().deriveFont(10.0f).deriveFont(Font.BOLD));
			pdTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 0, 5));
			
			//set up avai date Label
			sdf = new SimpleDateFormat("MMM dd YYYY,  HH:mm");
			postedDate = new JLabel(sdf.format(new Date(anm.getCreatedDTM().getTime())));
			postedDate.setFont(jlTitle.getFont().deriveFont(10.0f));
			postedDate.setForeground(new Color(40,40,40));
			postedDate.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
			
			//set up view button
			viewButton = new JButton ("View");
			viewButton.setFont(viewButton.getFont().deriveFont(12.0f));
			viewButton.setForeground(Color.BLUE);
			viewButton.setOpaque(false);
			viewButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			viewButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//Set up the panel
			setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			if(anm.getIsRead()) setBackground(Color.WHITE);
			else setBackground(Color.LIGHT_GRAY);
			setBorder(BorderFactory.createLineBorder(new Color(170,170,170), 1));
			setOpaque(true);
			setLayout(new GridBagLayout());
			
			//add the Title
			GridBagConstraints cf = new GridBagConstraints();
			cf.fill = GridBagConstraints.HORIZONTAL;
			cf.anchor = GridBagConstraints.FIRST_LINE_START;
			cf.gridwidth = 3;
			add(jlTitle,cf);
			
			//add "Posted date" title:
			GridBagConstraints cp = new GridBagConstraints();
			cp.insets = new Insets(0,0,10,0);
			cp.gridx = 0;
			cp.gridy = 1;
			cp.anchor = GridBagConstraints.LAST_LINE_START;
			add(pdTitle,cp);
			
			//add the posted date
			GridBagConstraints cd = new GridBagConstraints();
			cd.insets = new Insets(0,0,10,0);
			cd.gridx = 1;
			cd.gridy = 1;
			cd.anchor = GridBagConstraints.LAST_LINE_START;
			add(postedDate,cd);
			
			
			//add the view button
			GridBagConstraints cb = new GridBagConstraints();
			cb.gridx = 2;
			cb.gridy = 1;
			cb.weightx = 1.0;
			cb.anchor = GridBagConstraints.LAST_LINE_END;
			add(viewButton,cb);
			
			
			jpAnmContent = new JPanel(new BorderLayout());
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 2;
			c.gridwidth = 3;
			c.weighty = 1.0;
			c.insets = new Insets(0,10,10,10);
			c.fill = GridBagConstraints.BOTH;
			c.anchor = GridBagConstraints.FIRST_LINE_START;
			add(jpAnmContent,c);
			
			//Add View AL:
			viewButton.addActionListener(new ViewActionListener());
		}
		
		public class ViewActionListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				if(viewButton.getText().equals("View")){
					viewButton.setText("Close");
					JTextArea jtfContent = new JTextArea(anm.getAnnouncementContent());
					jtfContent.setEditable(false);
					JScrollPane scr = new JScrollPane(jtfContent);
					scr.setPreferredSize(new Dimension(500,200));
					jpAnmContent.add(scr,BorderLayout.CENTER);
					jpAnmContent.revalidate();
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+150));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+150));
					
					if(!anm.getIsRead()){
						anm.setIsRead(true);
						StudentAnnouncementActivities.setRead(CmsPresenter.user.getUsername(), anm.getAnnouncementID());
					}
					
				}
				else{
					viewButton.setText("View");
					jpAnmContent.removeAll();
					jpAnmContent.revalidate();
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setBackground(Color.WHITE);
				}
				
			}
			
		}
		
	}
	
}
